package indi.zhifa.recipe.bailan.framework.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * Description: 
 * @Copyright: Copyright (c) 2020-2028 北京华咨电力科技发展有限公司 All rights reserved.
 * @author: 褚智勇(hataksumo@163.com)
 * @date:  2022/1/26 3:29
 */
public class MethodUtils {
    public static String getCol(int pCol){
        Stack<Integer> st = new Stack<>();
        while (pCol>=0){
            st.push(pCol%26);
            pCol = pCol / 26 - 1;
        }
        StringBuilder sb = new StringBuilder();
        while (!st.isEmpty()){
            sb.append((char)(st.pop() + 'A'));
        }
        return sb.toString();
    }

    public static String encodeURIComponent(String input,boolean pErase) {
        Map<Integer,String> rpMap = new HashMap<Integer,String>(){{
            put('+'-0,"%2B");
            put(' '-0,"%20");
            put('/'-0,"%2F");
            put('?'-0,"%3F");
            put('%'-0,"%25");
            put('#'-0,"%23");
            put('&'-0,"%26");
            put('='-0,"%3D");
        }};
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<input.length();i++){
            char c = input.charAt(i);
            if(rpMap.containsKey(c-0)){
                if(!pErase){
                    sb.append(rpMap.get(c-0));
                }
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static Integer byte2int(byte[] bytes){
        if(null == bytes){
            return null;
        }
        int values = 0;
        int len = Math.min(bytes.length,4);
        for (int i = 0; i < len; i++) {
            values <<= 8;
            values |= (bytes[i] & 0xff);
        }
        return values;
    }

    public static Long byte2Long(byte[] bytes){
        if(null == bytes){
            return null;
        }
        long values = 0;
        int len = Math.min(bytes.length,8);
        for (int i = 0; i < len; i++) {
            values <<= 8;
            values |= (bytes[i] & 0xff);
        }
        return values;
    }
}
